package com.yaolaizaixian.merchant.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by ylzx on 2017/3/20.
 */
public class DateUtils {


    private static String mYear; // 当前年
    private static String mMonth; // 月
    private static String mDay;
    private static String mWay;
    public static String mMonth1;
    private static int month1;
    private static String Day;

    /**
     * 获取当前日期几月几号
     */
    public static String getDateString() {


        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {
            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));
        }
        return mMonth + "月" + mDay + "日";
    }


    /**
     * 获取当前年月日
     *
     * @return
     */
    public static String StringData() {


        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth)))) {

            mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (Integer.parseInt(mMonth))));

        }
        return mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
    }


    /**
     * 获取今天往前一周的日期（几月几号）
     * <p/>
     */
    public static List<String> getSevendate() {

        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));


        for (int i = 0; i < 7; i++) {

            mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) - i);// 获取当前日份的日期号码


            if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {

                mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
            }

            if (Integer.parseInt(mDay) < 10) {

                mDay = "-0" + mDay.replace("-","");
            }
            else {

                mDay = "-" + mDay;
            }

            //如果月份小于10
            if (Integer.parseInt(mMonth) < 10) {
                mMonth = "-0" + mMonth;
            }
            else {

                mMonth = "-" + mMonth;
            }

            //拼接一个  月  日
            String date = mMonth + mDay;

            dates.add(date);
        }

        return dates;
    }


    public static  String getStatetime() throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -7);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);
        return preMonday;
    }


    //获取当前月份
    public static String getMonth() {

        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);// 获取当前日份的日期号码

        int month = c.get(Calendar.MONTH) + 1;// 获取当前月份
        if (month == 1) {
            month1 = 12;
        } else {
            month1 = month - 1;
        }

        for (int i = 0; i < MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), month1); i++) {

            if (Integer.parseInt(mDay) > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1))) {

                mDay = String.valueOf(MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), (i + 1)));
            }
            if (Integer.parseInt(mDay) < 10) {
                Day = "-0" + mDay;
            } else {
                Day = "-" + mDay;
            }
            if (month1 < 10) {
                mMonth1 = "-0" + month1;
            } else {
                mMonth1 = "-" + month1;
            }

            String date = mMonth1 + Day;
            dates.add(date);
        }


        String s = dates.get(dates.size() - 1);

        String date = mYear + s;

        return date;
    }


    public static List<String> get7date() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        String date = sim.format(c.getTime());
        dates.add(date);
        for (int i = 0; i < 6; i++) {
            c.add(java.util.Calendar.DAY_OF_MONTH, 1);
            date = sim.format(c.getTime());
            dates.add(date);
        }
        return dates;
    }

    /**
     * 得到当年当月的最大日期
     **/
    public static int MaxDayFromDay_OF_MONTH(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);//注意,Calendar对象默认一月为0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }

    /**
     * 获取今天往后一天的日期（几月几号）
     */
    public static String getdate(String date) {

        mYear = date.substring(0, 4);// 获取当前年份

        if (date.substring(5, 6).equals("0")) {
            mMonth = date.substring(6, 7);// 获取当前月份
        } else {
            mMonth = date.substring(5, 7);// 获取当前月份
        }

        if (date.substring(8, 9).equals("0")) {
            mDay = date.substring(9, 10);// 获取当前日份的日期号码
        } else {
            mDay = date.substring(8, 10);// 获取当前日份的日期号码
        }

        if (Integer.parseInt(mDay)+1 > MaxDayFromDay_OF_MONTH(Integer.parseInt(mYear), Integer.parseInt(mMonth))) {
            if (mMonth.equals("12")){
                mMonth=String.valueOf(1);
                mYear=String.valueOf(Integer.parseInt(mYear)+1);
            }else {
                mMonth=String.valueOf(Integer.parseInt(mMonth)+1);
            }
            mDay = "1";
        }else {
           mDay=String.valueOf(Integer.parseInt(mDay)+1);
        }


        if (Integer.parseInt(mDay) < 10) {
            mDay = "-0" + mDay;
        } else {
            mDay = "-" + mDay;
        }
        if (Integer.parseInt(mMonth) < 10) {
            mMonth = "-0" + mMonth;
        } else {
            mMonth = "-" + mMonth;
        }

        String date1 = mYear+ mMonth + mDay;

        return date1;
    }
}
